//用户管理模块
import java.sql.*;
import java.util.Scanner;

public class UserManager {
    private Scanner scanner = new Scanner(System.in);

    public void register() {
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        System.out.print("请输入角色（admin 或 reader）：");
        String role = scanner.nextLine();

        String sql = "INSERT INTO users (username, password, role) VALUES (?, ?, ?)";
        try (Connection conn = DBHelper.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, role);
            pstmt.executeUpdate();
            System.out.println("注册成功！");
        } catch (SQLException e) {
            System.out.println("注册失败：" + e.getMessage());
        }
    }

    public boolean login() {
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        try (Connection conn = DBHelper.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                System.out.println("登录成功！欢迎：" + username);
                return true;
            } else {
                System.out.println("用户名或密码错误。");
                return false;
            }
        } catch (SQLException e) {
            System.out.println("登录失败：" + e.getMessage());
            return false;
        }
    }
}